package com.kuaimacode.kframework.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.api.vo.request.DrawApplyReq;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.constants.MasterConstants;
import com.kuaimacode.kframework.mybatis.mapper.user.UserDrawApplyMapper;
import com.kuaimacode.kframework.mybatis.models.sysparam.SysParam;
import com.kuaimacode.kframework.mybatis.models.user.UserDrawApply;
import com.kuaimacode.kframework.util.PrimaryIDUtil;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
;import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @author 12786 on 2019/3/30.
 * @version 1.0
 */
@Service
public class UserDrawApplyService extends ServiceImpl<UserDrawApplyMapper,UserDrawApply> {

    @Autowired
    SysParamService paramService;



    public PageInfo<UserDrawApply> getDrawHistList(Integer pageNum, Integer pageSize,Long userId,String applyStatus,String date){
        PageHelper.startPage(pageNum, pageSize,true,false,null);
        QueryWrapper<UserDrawApply> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("status", "1");
        if(StringUtils.isNotEmpty(date)){
            queryWrapper.le("date_format(create_dt,'%Y-%m')",date);
        }
        queryWrapper.orderByDesc("apply_date");
        return new PageInfo<UserDrawApply>((List<UserDrawApply>)baseMapper.selectList(queryWrapper));
    }



    @Transactional
    public boolean saveDrawApply(DrawApplyReq req,Long userId,BigDecimal tranAmount){
        //手续费比例
        SysParam sysParam = paramService.findByName("WITHDRAW_RATE");
        UserDrawApply apply = new UserDrawApply();
        Date date = new Date();
        apply.setAccountId(req.getAccountId());
        //手续费比例
        BigDecimal ratioBrokerage = new BigDecimal(sysParam.getValue()).multiply(new BigDecimal("0.01"));
        //所收手续费
        BigDecimal brokerage = new BigDecimal(req.getApplyAmount()).multiply(ratioBrokerage);
        //实际金额
        BigDecimal realityAmount = new BigDecimal(req.getApplyAmount()).subtract(brokerage).setScale(2,BigDecimal.ROUND_HALF_DOWN);
        if ("2".equals(req.getAccountType())&&StringUtils.isNotEmpty(req.getAccountType())){
            apply.setAccountType(2);
            apply.setRemark(MasterConstants.REMARK);
        }else{
            apply.setAccountType(1);
            apply.setRemark(MasterConstants.NOMAR_REMARK);
        }
        apply.setApplyAmount(req.getApplyAmount());
        apply.setApplyNo(PrimaryIDUtil.getId());
        apply.setApplyStatus(MasterConstants.APPLYING);
        apply.setApplyDate(date);
        apply.setCreateDt(date);
        apply.setUpdateDt(date);
        apply.setUserId(userId);
        apply.setStatus("1");
        apply.setTranAmount(tranAmount);
        apply.setRatio(ratioBrokerage);
        apply.setBrokerage(brokerage);
        apply.setRealityAmount(realityAmount);
         return  baseMapper.insert(apply)>0;
    }


}
